package com.zhj.leetcode;

/**
 * 力扣 141 环形链表
 * 给定一个链表，判断链表中是否有环
 * @author zhj
 */
public class Test141 {
    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(4);
        ListNode node4 = new ListNode(5);
        node.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node2;
        System.out.println(isRing(node));
    }

    private static boolean isRing(ListNode node) {
        if (node == null || node.next == null) {
            return false;
        }
        ListNode s = node;
        ListNode f = node;
        while (f != null && f.next != null) {
            s = s.next;
            f = f.next.next;
            if (s == f) {
                return true;
            }
        }
        return false;
    }
}
